// Copyright (c) 2022 CHANGLEI. All rights reserved.

import 'package:aircraft_war/pages/game_page.dart';
import 'package:flutter/widgets.dart';

/// Created by changlei on 2022/5/20.
///
/// 路由
enum Routes {
  /// 游戏页面
  game;

  WidgetBuilder get _builder {
    final WidgetBuilder builder;
    switch (this) {
      case Routes.game:
        builder = (context) => const GamePage();
        break;
    }
    return builder;
  }

  /// 初始路由
  static Routes get initialRoute {
    return Routes.game;
  }

  /// 路由表
  static Map<String, WidgetBuilder> get routes {
    final routes = Map.fromEntries(Routes.values.map((e) => MapEntry(e.name, e._builder)));
    routes[Navigator.defaultRouteName] = initialRoute._builder;
    return routes;
  }
}

/// 扩展
extension RoutesExtension on Routes {
  /// [Navigator.pushNamed]
  @optionalTypeArgs
  Future<T?> pushNamed<T extends Object?>(
    BuildContext context, {
    Object? arguments,
  }) {
    return Navigator.pushNamed<T>(context, name, arguments: arguments);
  }

  /// [Navigator.restorablePushNamed]
  @optionalTypeArgs
  String restorablePushNamed<T extends Object?>(
    BuildContext context, {
    Object? arguments,
  }) {
    return Navigator.restorablePushNamed<T>(context, name, arguments: arguments);
  }

  /// [Navigator.pushReplacementNamed]
  @optionalTypeArgs
  Future<T?> pushReplacementNamed<T extends Object?, TO extends Object?>(
    BuildContext context, {
    TO? result,
    Object? arguments,
  }) {
    return Navigator.pushReplacementNamed<T, TO>(context, name, arguments: arguments, result: result);
  }

  /// [Navigator.restorablePushReplacementNamed]
  @optionalTypeArgs
  String restorablePushReplacementNamed<T extends Object?, TO extends Object?>(
    BuildContext context, {
    TO? result,
    Object? arguments,
  }) {
    return Navigator.restorablePushReplacementNamed<T, TO>(context, name, arguments: arguments, result: result);
  }

  /// [Navigator.popAndPushNamed]
  @optionalTypeArgs
  Future<T?> popAndPushNamed<T extends Object?, TO extends Object?>(
    BuildContext context, {
    TO? result,
    Object? arguments,
  }) {
    return Navigator.popAndPushNamed<T, TO>(context, name, arguments: arguments, result: result);
  }

  /// [Navigator.restorablePopAndPushNamed]
  @optionalTypeArgs
  String restorablePopAndPushNamed<T extends Object?, TO extends Object?>(
    BuildContext context, {
    TO? result,
    Object? arguments,
  }) {
    return Navigator.restorablePopAndPushNamed<T, TO>(context, name, arguments: arguments, result: result);
  }

  /// [Navigator.pushNamedAndRemoveUntil]
  @optionalTypeArgs
  Future<T?> pushNamedAndRemoveUntil<T extends Object?>(
    BuildContext context,
    RoutePredicate predicate, {
    Object? arguments,
  }) {
    return Navigator.pushNamedAndRemoveUntil<T>(context, name, predicate, arguments: arguments);
  }

  /// [Navigator.restorablePushNamedAndRemoveUntil]
  @optionalTypeArgs
  String restorablePushNamedAndRemoveUntil<T extends Object?>(
    BuildContext context,
    RoutePredicate predicate, {
    Object? arguments,
  }) {
    return Navigator.restorablePushNamedAndRemoveUntil<T>(context, name, predicate, arguments: arguments);
  }
}
